package com.windea.study.kotlin.game.tankwar2.model

import com.windea.study.kotlin.game.tankwar2.*
import com.windea.study.kotlin.game.tankwar2.GameConfig.block
import com.windea.study.kotlin.game.tankwar2.core.*
import com.windea.study.kotlin.game.tankwar2.interfaces.*

/**大本营。*/
class Camp(
	override var x: Int,
	override var y: Int
) : Terrain(x, y), Blockable, Sufferable, Destroyable {
	override var width: Int = block * 2
	override var height: Int = (block * 1.5).toInt()
	override var health: Int = 12
	
	override fun draw() {
		//血量低于 6个时画的时 砖墙
		//血量低于 3个时画的时 没有墙
		when {
			health <= 3 -> {
				x = (GameConfig.width - block) / 2
				y = GameConfig.height - block
				width = block
				height = block
				Painter.drawImage("img/camp.gif", x, y)
			}
			health <= 6 -> {
				//绘制外围的砖块
				Painter.drawImage("img/wall_small.gif", x, y)
				Painter.drawImage("img/wall_small.gif", x + block / 2, y)
				Painter.drawImage("img/wall_small.gif", x + block, y)
				Painter.drawImage("img/wall_small.gif", x + block / 2 * 3, y)
				
				Painter.drawImage("img/wall_small.gif", x, y + block / 2)
				Painter.drawImage("img/wall_small.gif", x, y + block)
				
				Painter.drawImage("img/wall_small.gif", x + block / 2 * 3, y + block / 2)
				Painter.drawImage("img/wall_small.gif", x + block / 2 * 3, y + block)
				
				Painter.drawImage("img/camp.gif", x + block / 2, y + block / 2)
			}
			else -> {
				//绘制外围的砖块
				Painter.drawImage("img/steel_small.gif", x, y)
				Painter.drawImage("img/steel_small.gif", x + block / 2, y)
				Painter.drawImage("img/steel_small.gif", x + block, y)
				Painter.drawImage("img/steel_small.gif", x + block / 2 * 3, y)
				
				Painter.drawImage("img/steel_small.gif", x, y + block / 2)
				Painter.drawImage("img/steel_small.gif", x, y + block)
				
				Painter.drawImage("img/steel_small.gif", x + block / 2 * 3, y + block / 2)
				Painter.drawImage("img/steel_small.gif", x + block / 2 * 3, y + block)
				
				Painter.drawImage("img/camp.gif", x + block / 2, y + block / 2)
			}
		}
	}
	
	override fun onSuffer(other: Attackable, entities: MutableList<Entity>) {
		health -= other.attack
		entities += when(health) {
			3, 6 -> {
				val x = x - 32
				val y = y - 32
				arrayOf(
					Blast(x, y),
					Blast(x + block / 2, y),
					Blast(x + block, y),
					Blast(x + block / 2 * 3, y),
					Blast(x + block * 2, y),
					Blast(x, y + block / 2),
					Blast(x, y + block),
					Blast(x, y + block / 2 * 3),
					Blast(x + block * 2, y + block / 2),
					Blast(x + block * 2, y + block),
					Blast(x + block * 2, y + block / 2 * 3)
				)
			}
			else -> arrayOf()
		}
	}
	
	override fun onDestroy(entities: MutableList<Entity>) {
		entities += arrayOf(
			Blast(x - block / 2, y - block / 2),
			Blast(x, y - block / 2),
			Blast(x + block / 2, y - block / 2),
			Blast(x - block / 2, y),
			Blast(x, y),
			Blast(x + block / 2, y),
			Blast(x - block / 2, y + block / 2),
			Blast(x, y + 32),
			Blast(x + block / 2, y + block / 2)
		)
	}
	
	override fun isDestroyed(): Boolean {
		return health <= 0
	}
}
